<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>排列组合演示</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f7fc;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            flex-direction: column;
        }

        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 20px;
        }

        .container {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 80%;
            max-width: 500px;
            text-align: center;
        }

        input {
            padding: 10px;
            margin-bottom: 15px;
            width: 80%;
            font-size: 16px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        button {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            margin-bottom: 20px;
        }

        button:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 20px;
            padding: 10px;
            background-color: #f9f9f9;
            border: 1px solid #ddd;
            border-radius: 4px;
            max-height: 200px;
            overflow-y: auto;
        }

        .result p {
            margin: 5px 0;
            word-wrap: break-word;
        }
    </style>
</head>
<body>

    <h1>排列组合示例</h1>
    <div class="container">
        <h3>字符串排列组合</h3>
        <input type="text" id="strInput" placeholder="请输入字符串 (例如: abc)">
        <button onclick="calculateStrPermutation()">计算排列</button>

        <div class="result" id="strResult"></div>

        <h3>数组排列组合</h3>
        <input type="text" id="arrInput" placeholder="请输入数组 (例如: [1, 2, 3])">
        <button onclick="calculateArrPermutation()">计算排列</button>

        <div class="result" id="arrResult"></div>
    </div>

    <script>
        // 字符串排列组合函数
        function permutationStr(per) {
            if (per.length === 1) {
                return [per];
            }
            let res = [];
            [...per].forEach((v, i, str) => {
                const temp = [...str];
                const header = temp.splice(i, 1);
                permutationStr(temp).forEach(value => res.push([header, ...value].join('')));
            });
            return [...new Set(res)];
        }

        // 数组排列组合函数
        function permutationArr(per) {
            if (per.length === 1) {
                return [per];
            }
            let res = [];
            per.forEach((v, i, arr) => {
                let temp = [...arr];
                let header = temp.splice(i, 1);
                permutationArr(temp).forEach(val => res.push([...header, ...val]));
            });
            return [...new Set(res.map(value => JSON.stringify(value)))].map(val =>
                JSON.parse(val)
            );
        }

        // 计算字符串排列组合
        function calculateStrPermutation() {
            const input = document.getElementById('strInput').value.trim();
            if (input) {
                const result = permutationStr(input);
                displayResult(result, 'strResult');
            } else {
                alert('请输入一个字符串');
            }
        }

        // 计算数组排列组合
        function calculateArrPermutation() {
            const input = document.getElementById('arrInput').value.trim();
            try {
                const arr = JSON.parse(input);
                if (Array.isArray(arr)) {
                    const result = permutationArr(arr);
                    displayResult(result, 'arrResult');
                } else {
                    alert('请输入一个有效的数组');
                }
            } catch (e) {
                alert('请输入一个有效的数组');
            }
        }

        // 显示结果
        function displayResult(result, elementId) {
            const resultDiv = document.getElementById(elementId);
            resultDiv.innerHTML = ''; // 清空之前的结果
            if (result.length === 0) {
                resultDiv.innerHTML = '<p>没有结果</p>';
            } else {
                result.forEach(res => {
                    const p = document.createElement('p');
                    p.textContent = Array.isArray(res) ? JSON.stringify(res) : res;
                    resultDiv.appendChild(p);
                });
            }
        }
    </script>

</body>
</html>
